#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "delay.h"
#include "../HardWare/usart2.h"
//u8 USART2_SEND_BUF[USART2_SEND_BUF_LEN];
//u8 USART2_REC_BUF[USART2_REC_BUF_LEN];
//u8 USART2_SEND_SIZE = 0;
//u8 USART2_REC_SIZE = 0;

void usart2_init(u32 bound)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能串口2时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能串口2对应的GPIOA时钟
    USART_DeInit(USART2);
    
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;  
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA,&GPIO_InitStruct);
    //GPIOA.2 usart2 TX 引脚初始化

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA,&GPIO_InitStruct);
    //GPIOA.3  USART2 RX 引脚初始化

    USART_InitStruct.USART_BaudRate = bound;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART2,&USART_InitStruct);
    //USART2初始化

    USART_Cmd(USART2,ENABLE);  //开启串口2
	
    //注意,读取USARTx->SR能避免莫名其妙的错误  
    //同时解决复位、开机时串口第一个字符串发送失败问题
	USART_GetFlagStatus(USART2,USART_FLAG_TC);
    USART_GetFlagStatus(USART2,USART_FLAG_RXNE);
}
